'''
Created on Jun 23, 2011

@author: oabalbin
'''

#import exome.variantEval.snps_annotator as ann
from collections import defaultdict, deque
from optparse import OptionParser

from exome.jobs.base import JOB_SUCCESS, JOB_ERROR
from exome.jobs.job_runner import qsub_cac, qsub_loc, run_local
from exome.jobs.config import ExomePipelineConfig, ExomeAnalysisConfig
from exome.variantEval.variant_intersector import variant_isec, SNPs_isec
from exome.variantEval.snps_annotator import create_snps_annotation

# Global Variables
NODE_MEM=45000.0
NODE_CORES=12
SINGLE_CORE=1
MEM_PER_CORE= int(float(NODE_MEM) / NODE_CORES)
# wt=walltime
WT_SHORT= "24:00:00"
WT_LONG= "60:00:00" #"100:00:00"


def germline_analysis(configrun, analysis, snps_vcf, snps_annot_vcf,
                      jobrunfunc, deps_list, do_dbSNP_isec=True):
    '''
    This function does a) intersect snps_vcf with dbSNP if isec=True
    Annotates the isec file.
    '''
    extra_mem = configrun.gatk_use_mem
    genomes = configrun.genomes['human']
    ref_genome, snpdb_vcf, indeldb_file = genomes.gatk_ref_genome, genomes.snpdb, \
                                         genomes.indeldb
    hapmap_vcf, tgk_vcf = genomes.hapmap, genomes.OneKgenomes 
    #vcfCodings parameters
    vcfCodSNP_genome = genomes.vcf_ref_genome
    annot_genelist = configrun.annot_genelist
    path_to_vcfCodSNPs = configrun.vcf_annot_path
    # email
    my_email=configrun.email_addresses
    # 
    jobn=analysis.name    
    # variant_isec(analysis, configrun, query_vcf_file, jobrunfunc, do_complement)
    snps_vcf_isec = snps_vcf.replace('.vcf','isec_known.vcf')
    snps_vcf_isec_annot= snps_vcf.replace('.vcf','.annot.vcf')

    do_complement=True        
    jobidvcfh = variant_isec(analysis, configrun, snps_vcf, jobrunfunc, do_complement, snps_vcf_isec)

    commandA = create_snps_annotation(snps_vcf_isec, snps_vcf_isec_annot, 
                            snps_vcf_isec_annot, vcfCodSNP_genome, 
                            annot_genelist, path_to_vcfCodSNPs)
        
    jobidannA = jobrunfunc('germ.'+jobn, commandA, SINGLE_CORE, cwd=None, walltime=WT_SHORT, pmem=extra_mem, 
                              deps=jobidvcfh, stdout=None, email_addresses=my_email)
    
    return jobidannA
    

if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-r", "--config_file", dest="config_file",
                            help="file with run configuration")
    optionparser.add_option("-a", "--analysis_file", dest="analysis_file",
                            help="file with experiment configuration") 
    optionparser.add_option("-f", "--vcf_file", dest="vcf_file",
                            help="file with experiment configuration") 
    
    optionparser.add_option("--local_cluster", dest="local_cluster", action="store_true", default=False)      
    optionparser.add_option("--local", dest="local", action="store_true", default=False)
    optionparser.add_option("--cluster", dest="cluster", action="store_true", default=False)
    optionparser.add_option("-p", "--processes", type=int, dest="num_processors", default=1)

    (options, args) = optionparser.parse_args()    

    config = ExomePipelineConfig()
    config.from_xml(options.config_file)
    analysis = ExomeAnalysisConfig()
    analysis.from_xml(options.analysis_file, config.output_dir)
    #Default when called from the command line
    depends=None
    
    if not (options.local ^ options.cluster ^ options.local_cluster):
        optionparser.error("Must set either --local, --cluster or --local_cluster to run job")
    if options.local:
        jobrunfunc = run_local
    elif options.cluster:
        jobrunfunc = qsub_cac
    elif options.local_cluster:
        jobrunfunc = qsub_loc
    
    
    germline_analysis(config, analysis, options.vcf_file, options.vcf_file.replace('.vcf','.annot.vcf'),
                      jobrunfunc, depends, do_dbSNP_isec=True)
       

